// pages/index/shop/shop.js
const app = getApp()
import Notify from '@vant/weapp/notify/notify';
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		found: [],
		lost: [],
		mine: [],
		info: {},
		modalName: null,
		temp: {},
		body: '',
		radio: '',
    list: [],
    loadModel:true
	},
	onLoad(options) {},
	onShow() {
		var user = wx.getStorageSync('user')
		this.setData({
			info: user
		})
		this.getall()
	},
	getall() {
		var user = this.data.info
		//一次请求获取全部的信息，减少网络请求
		app.request({
			url: app.globalData.apiUrl + '/found/all/' + user.id,
			method: 'GET',
		}).then(res => {
			if (res.statusCode == 200) {
				this.setData({
					found: res.data.data.found,
					lost: res.data.data.lost,
          mine: res.data.data.mine,
          loadModel:false
				})
			} else {
        this.setData({
          loadModel: false
        })
				Notify('服务错误，稍后重试');
			}
		})
	},
	//跳转到新增界面
	gotoadd() {
		wx.navigateTo({
			url: '/pages/index/found/add/add',
		})
	},
	delete(e) {
		console.log(e)
		app.request({
			url: app.globalData.apiUrl + '/found/' + e.target.dataset.id,
			method: 'DELETE',
		}).then(res => {
			if (res.statusCode == 200) {
				Notify({
          type: 'primary',
          message: '删除成功'
        })
				this.getall()
			} else {
				loadModel: false
				Notify('服务错误，稍后重试');
			}
		})
	},
	showModal(e) {
		console.log(e)
		this.setData({
			modalName: e.currentTarget.dataset.target,
			temp: e.currentTarget.dataset.item
		})
		console.log(this.data)
	},
	hideModal(e) {
		this.setData({
			modalName: null,
			temp: null,
			body: '',
			radio: '',
			list: ''
		})
		console.log(this.data)
	},
	get1() {
		var get = JSON.stringify({
			type: 0,
			uid: this.data.info.id,
			fid: this.data.temp.id,
			body: this.data.message
		})
		app.request({
			url: app.globalData.apiUrl + '/get/' + get,
			method: 'POST',
		}).then(res => {
			if (res.statusCode == 200) {
				Notify({
          type: 'primary',
          message: '申请成功'
        })
				this.hideModal()
			} else {
				loadModel: false
				Notify('服务错误，稍后重试');
			}
		})
	},
	get2() {
		var get = JSON.stringify({
			type: 1,
			uid: this.data.info.id,
			fid: this.data.temp.id,
			body: this.data.message
		})
		app.request({
			url: app.globalData.apiUrl + '/get/' + get,
			method: 'POST',
		}).then(res => {
			if (res.statusCode == 200) {
				Notify({
          type: 'primary',
          message: '通知成功'
        })
				this.hideModal()
			} else {
				loadModel: false
				Notify('服务错误，稍后重试');
			}
		})
	},
	showModal2(e) {
		console.log(e)
		this.setData({
			modalName: e.currentTarget.dataset.target,
			temp: e.currentTarget.dataset.item
		})
		app.request({
			url: app.globalData.apiUrl + '/get/' + this.data.temp.id,
			method: 'GET',
		}).then(res => {
			if (res.statusCode == 200) {
				this.setData({
					list: res.data.data
				})
			} else {
				loadModel: false
				Notify('服务错误，稍后重试');
			}
		})
	},
	finish() {
		console.log('1111')
		var choice = this.data.radio
		console.log(choice)
		if(choice==''){
			Notify('请选择后在进行提交');
		}else{
			var found=JSON.stringify({
				id:this.data.temp.id,
				gid:choice,
				done:1
			})
			app.request({
				url: app.globalData.apiUrl + '/found/' + found,
				method: 'PUT',
			}).then(res => {
				if (res.statusCode == 200) {
					Notify({
						type: 'primary',
						message: '操作成功'
					})
					this.hideModal()
					this.getall()
				} else {
					loadModel: false
					Notify('服务错误，稍后重试');
				}
			})
		}
	},
	onChange(event) {
		this.setData({
			radio: event.detail,
		});
	},

})